<template>
  <div>
    <el-forms :modul="ruleForm"> </el-forms>
    <el-form :model="ruleForm" :rules="rules" ref="form" label-width="80px">
      <el-form-item label="手机号" prop="moduil">
        <el-input v-model="ruleForm.moduil"></el-input>
      </el-form-item>
      <el-form-item label="验证码" prop="phone">
        <el-input v-model="ruleForm.phone"></el-input>
      </el-form-item>
      <!-- 特别说明 -->
      <el-form-item label="特别说明">
        <el-input v-model="ruleForm.other"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">登录</el-button>
        <el-button @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import ElForms from './child'
export default {
  components: {
    ElForms
  },
  data () {
    // 定义 自定义校验函数（复杂校验）
    // rule:校验规则
    // value:表单项输入的值
    // callback：（函数）决定是否通过校验 校验失败，调用callback（new Error('错误说明)）
    //
    const validateMobile = (rule, value, callback) => {
      if (!/^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/.test(value)) {
        callback(new Error('手机号格式不对！'))
      } else {
        callback()
      }
    }
    return {
      ruleForm: {
        moduil: '',
        phone: '',
        other: ''
      },
      // 校验规则的属性名和要校验的表单数据的属性名一模一样
      rules: {
        moduil: [
          // blur 鼠标失去焦点开始校验
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          // { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
          { validator: validateMobile, trigger: 'blur' }
        ],
        // change 一遍输入一边校验
        phone: [
          { required: true, message: '请输入活动名称', trigger: 'change' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    async onSubmit () {
      try {
        await this.$refs.form.validate()
        console.log('登录', this.ruleForm)
      } catch (e) {
        console.log(e)
      }
    },
    // 重置
    reset () {
      this.ruleForm.other = ''
      this.$refs.form.resetFields()
    }
  }
}
</script>

<style lang="less" scoped>
</style>
